/**
 * 
 */
package com.pine.sunshineelder.exit.service.impl;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.scripting.xmltags.TrimSqlNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dao.AccountDao;
import com.pine.sunshineelder.common.dao.ElderDao;
import com.pine.sunshineelder.common.dao.RecordExitDao;
import com.pine.sunshineelder.common.entity.ElderEntity;
import com.pine.sunshineelder.common.entity.RecordExitEntity;
import com.pine.sunshineelder.common.exception.ThisSystemException;
import com.pine.sunshineelder.common.validator.ValidateHelper;
import com.pine.sunshineelder.exit.ao.ExitAo;
import com.pine.sunshineelder.exit.service.ExitService;

import lombok.NonNull;

/**
 * @author 苟瑞雪
 *
 */
@Service
public class ExitServiceImpl implements ExitService,StringHelper{
	@Autowired
	RecordExitDao redao;
	@Autowired
	ElderDao edao;
	@Autowired
	AccountDao adao;

	@Override
	public RecordExitEntity exit(@NonNull  ExitAo ao) throws Exception {
		String elderId=ValidateHelper.$("elderId", ao.getElderId());
		ElderEntity ee = edao.selectById(elderId);
		if(ee==null) {
			throw new ThisSystemException("illegal-id", "非法elderId");
		}
		String exitReason=trimAsNull(ao.getExitReason());
		
		Integer exitType=ao.getExitType();
		
		LocalDate exitTime=ao.getExitTime();
		
		String createUserId=trimAsNull(ao.getCreateUserId());
	
		RecordExitEntity re=new RecordExitEntity();
		re.setId(uuid());
		re.setElderId(elderId);
		re.setExitReason(exitReason);
		re.setExitTime(exitTime);
		re.setExitType(exitType);
		re.setCreateUserId(createUserId);
		redao.insert(re);
		adao.delete(elderId);
		return re;
	}

}
